package com.yotoodesign.core.util;

import java.io.File;

import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

import com.yotoodesign.core.common.Memory;

/*
 * 内、外存空间大小检查
 */
public class MemoryUtil {
	public Memory readSDCard() {
		Memory result = new Memory(); 
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			File sdcardDir = Environment.getExternalStorageDirectory();
			StatFs sf = new StatFs(sdcardDir.getPath());
			long blockSize = sf.getBlockSize();
			long blockCount = sf.getBlockCount();
			long availCount = sf.getAvailableBlocks();
			
			result.setBlockSize(blockSize);
			result.setBlockCount(blockCount);
			result.setAvailCount(availCount);
			result.setTotalSize(blockSize * blockCount / 1024);
			result.setAvailSize(availCount* blockSize / 1024);
			
			Log.d("", "block大小:" + blockSize + ",block数目:" + blockCount
					+ ",总大小:" + blockSize * blockCount / 1024 + "KB");
			Log.d("", "可用的block数目：:" + availCount + ",剩余空间:" + availCount
					* blockSize / 1024 + "KB");
		}
		return result;
	}

	public Memory readSystem() {
		Memory result = new Memory();
		File root = Environment.getRootDirectory();
		StatFs sf = new StatFs(root.getPath());
		long blockSize = sf.getBlockSize();
		long blockCount = sf.getBlockCount();
		long availCount = sf.getAvailableBlocks();
		
		result.setBlockSize(blockSize);
		result.setBlockCount(blockCount);
		result.setAvailCount(availCount);
		result.setTotalSize(blockSize * blockCount / 1024);
		result.setAvailSize(availCount* blockSize / 1024);
		
		Log.d("", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:"
				+ blockSize * blockCount / 1024 + "KB");
		Log.d("", "可用的block数目：:" + availCount + ",可用大小:" + availCount
				* blockSize / 1024 + "KB");
		return result;
	}
}
